import { Body, Controller, Get, HttpCode, Post, Query, Param, Headers } from '@nestjs/common';
import { UserService } from './user.service';
import { ApiResponse, ApiOperation, ApiTags, ApiBody, ApiQuery } from '@nestjs/swagger';
import { CreateUserDto, GetUserPage, LoginDto, UpdatePassWordDto, UpdateUserInfoDto } from './dto/user.dto';
import { User } from './entities/user.entity';
import { Public } from 'src/common/auth/public.decorator';

@ApiTags('用户')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) { }
  /** 用户列表 */
  @HttpCode(200)
  @Get('/getUserPage')
  @ApiResponse({
    status: 200,
    description: '用户列表',
    type: [User],
  })
  @ApiOperation({
    tags: ['获取用户列表'],
    summary: '获取用户列表',
    operationId: 'getUserPage',
  })
  getUserPage(@Query() params: GetUserPage) {
    return this.userService.getUserPage(params);
  }

  @HttpCode(200)
  @Post('/register')
  @ApiQuery({
    description: '用户注册',
    type: CreateUserDto,
  })
  @ApiBody({ description: '用户注册', type: CreateUserDto })
  @ApiOperation({ summary: '用户注册', operationId: 'register' })
  @ApiResponse({ status: 200, type: User })
  @Public()
  register(@Body() body: CreateUserDto) {
    return this.userService.createUser(body);
  }

  @HttpCode(200)
  @Post('/login')
  @ApiQuery({
    description: '登录',
    type: LoginDto,
  })
  @ApiBody({ description: '登录', type: LoginDto })
  @ApiOperation({ summary: '登录', operationId: 'login' })
  @Public()
  login(@Body() body: LoginDto) {
    return this.userService.login(body);
  }

  /** ID获取用户详情 */
  @HttpCode(200)
  @Get('/getUserById')
  @ApiResponse({
    status: 200,
    description: 'ID获取用户详情',
    type: User,
  })
  @ApiOperation({
    tags: ['ID获取用户详情'],
    summary: 'ID获取用户详情',
    operationId: 'getUserById',
  })
  async getUserById(@Param() id) {
    const data = await this.userService.getUserById(id);
    return data;
  }

  /** 修改密码 */
  @HttpCode(200)
  @Post('/updatePassWord')
  @ApiQuery({
    description: '修改密码',
    type: UpdatePassWordDto,
  })
  @ApiBody({ description: '修改密码', type: UpdatePassWordDto })
  @ApiOperation({ summary: '修改密码', operationId: 'updatePassWord' })
  @ApiResponse({ status: 200, type: String })
  updatePassWord(@Body() body: UpdatePassWordDto, @Headers() headers) {
    return this.userService.updatePassWord(body, headers);
  }

  /** 更新用户信息 */
  @HttpCode(200)
  @Post('/updateUserInfo')
  @ApiQuery({
    description: '更新用户信息',
    type: UpdateUserInfoDto,
  })
  @ApiBody({ description: '更新用户信息', type: UpdateUserInfoDto })
  @ApiOperation({ summary: '更新用户信息', operationId: 'updateUserInfo' })
  @ApiResponse({ status: 200, type: String })
  updateUserInfo(@Body() body: UpdateUserInfoDto, @Headers() headers) {
    return this.userService.updateUser(body, headers);
  }
}
